<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName" autocomplete="off" class="layui-input">
                            </div>
                        </div>                                                   
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i>查 询</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
			{{# if(d.state==1){ }}
    		<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="toUse">启用</a>
			{{# } }}
			{{# if(d.state==0){ }}
    		<a class="layui-btn layui-btn-xs" lay-event="toUnUse">禁用</a>
			{{# } }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="initPassword">重置密码</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate=layui.laydate,
            table = layui.table;
        //加载下拉列表
        layer.ready(function(){
     	   $.post("../sysRole/roleList", function (data) {
     		    var info = "<option value=''>请选择</option>";
     		    for (var i = 0; i < data.length; i++) {    
     		        info += "<option value=" + data[i].roleId + ">" + data[i].roleName + "</option>";        
     		    }
     		    $("#roleType").append(info);
     		    form.render('select');
     		 })
         });        

        table.render({
            elem: '#currentTableId',
            url: '../sysMemberInfo/list',
            toolbar: '#toolbarDemo',
            contentType: "application/json",
            method: 'POST',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "numbers", title: '序号'},
                {field: 'memberId', title: 'ID',hide:true,align: "center"},
                {field: 'userName',title: '用户账号',align: "center"},
                {field: 'roleName',title: '用户角色',align: "center"},
                {field: 'addTime',title: '注册时间',sort: true,align: "center"},
                {field: 'state', title: '账号状态', sort: true,align: "center",
                	templet: function (item) {                        
                    	if (item.state == 0) {
                        	return "正常"
                    	}else if(item.state == 1){
                        	return "禁用"
                    	}
                    }},               
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                ,  where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '../sysMemberInfo/add',
                    end: function () {
                        table.reload('currentTableId');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }          
        });

        table.on('tool(currentTableFilter)', function (obj) {
           var data = obj.data;
           if (obj.event === 'initPassword') {
                layer.confirm('真的要重置吗，密码默认重置为admin123', function (index) {
                    $.ajax({
                        type: "POST", //提交方式
                        url: "../sysMemberInfo/initPassword",//路径
                        data: {"id": data.memberId},//数据，这里使用的是Json格式进行传输
                        success: function (result) {//返回数据根据结果进行相应的处理
                            layer.msg(result.msg);
                            table.reload("currentTableId")
                        }
                    });
                    layer.close(index);
                });
            }else if (obj.event === 'toUse') {
                $.ajax({
                    type: "POST", //提交方式
                    url: "../sysMemberInfo/toUse",//路径
                    data: {"id":data.memberId},//数据，这里使用的是Json格式进行传输
                    success: function (result) {//返回数据根据结果进行相应的处理
                    	var index = layer.msg(result.msg, {
                            time: 1000 //如果不配置，默认是3秒）
                        },function () {
                            // 关闭弹出层
                            layer.close(index);
                            window.location.reload();
                        })                            
                    }
                });
                layer.close(index);
            }else if (obj.event === 'toUnUse') {
                $.ajax({
                    type: "POST", //提交方式
                    url: "../sysMemberInfo/toUnUse",//路径
                    data: {"id":data.memberId},//数据，这里使用的是Json格式进行传输
                    success: function (result) {//返回数据根据结果进行相应的处理
                    	var index = layer.msg(result.msg, {
                            time: 2000 //如果不配置，默认是3秒）
                        },function () {
                            // 关闭弹出层
                            layer.close(index);
                            window.location.reload();
                        })                            
                    }
                });
                layer.close(index);
            }
        });
    });
</script>

</body>
</html>